package cn.itheima.web;

import cn.itheima.domain.Section;
import cn.itheima.domain.User;
import cn.itheima.service.SectionService;
import cn.itheima.service.SectionServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author: cuisensen
 * data: 2018/7/3.
 * time: 15:52.
 */
@WebServlet("/section")
public class SectionServlet extends BaseServlet {
    public void addSection(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String sname=null!=request.getParameter("sname")?request.getParameter("sname"):"";
        HttpSession session=request.getSession();
        PrintWriter out = response.getWriter();
        List<Section> sections= (List<Section>) session.getAttribute("sections");
        boolean success=false;
        Section section=new Section();
        section.setSname(sname);
        section.setUid(((User)session.getAttribute("user")).getCode());
        if(!sections.contains(section)){
            SectionService ss=new SectionServiceImp();

            ss.insertSection(section);
            sections.add(section);
            success=true;
        }
        String json="{\"success\":"+success+"}";
        out.write(json);
    }
}
